package com.list_builder;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;

import com.list_builder.comparators.DateComparator;
import com.list_builder.comparators.GenderComparator;
import com.list_builder.comparators.LastNameComparator;

public class ListBuilder {
	
	public static final String COMMA_DELIMITER = "comma";
	public static final String PIPE_DELIMITER = "pipe";
	public static final String SPACE_DELIMITER ="space";
	
	public static String getDelimiter(String delimiterName)
	{
		if(delimiterName.equals(COMMA_DELIMITER))
		{
			return ",";
		}
		else if(delimiterName.equals(PIPE_DELIMITER))
		{
			return "\\|";
		}
		else if(delimiterName.equals(SPACE_DELIMITER))
		{
			return " ";
		}
		else
		{
			return null;
		}
	}
	
	// First argument is an input file containing the name of the file to parse and data about the file
	// Second argument is the name of the output file
	public static void main(String[] args)
	{
		ArrayList<InputFile> inputFileList = new ArrayList<InputFile>();
		ArrayList<Person> peopleList = new ArrayList<Person>();
		
		try{
			
			// Read in the type of files we are going to be parsing, their location, and their properties
			BufferedReader propertyReader = new BufferedReader(new FileReader(args[0]));
			
			String fileListing;
			
			while((fileListing = propertyReader.readLine()) != null)
			{
				String parsedLine[] = fileListing.split(",");
				inputFileList.add(new InputFile(getDelimiter(parsedLine[0]), 
						Integer.parseInt(parsedLine[1]), Integer.parseInt(parsedLine[2]), Integer.parseInt(parsedLine[3]), 
						Integer.parseInt(parsedLine[4]), Integer.parseInt(parsedLine[5]), parsedLine[6], parsedLine[7]));
			}
			
			// First build the list of people
			for(int i = 0; i < inputFileList.size(); i++)
			{
				InputFile input = inputFileList.get(i);
				BufferedReader fileReader = new BufferedReader(new FileReader(input.getFileName()));
				
				String line;
				
				try{
					
					DateFormat formatter = new SimpleDateFormat(input.getDateFormat());
					
					while((line = fileReader.readLine()) != null)
					{
						String[] parsedLine = line.split(input.getDelimiter());
						Person p = new Person();
						p.setDateOfBirth(formatter.parse(parsedLine[input.getDobIndex()]));
						p.setFavoriteColor(parsedLine[input.getColorIndex()].trim());
						p.setFirstName(parsedLine[input.getfNameIndex()].trim());
						p.setLastName(parsedLine[input.getlNameIndex()].trim());
						p.setGender(parsedLine[input.getGenderIndex()].trim());
						peopleList.add(p);
					}
					
				} catch (ParseException e) {

					System.err.println(e.getMessage());
				}
				finally
				{
					fileReader.close();
				}
			}
			
			// Then output to a file
			BufferedWriter fileWriter = new BufferedWriter(new FileWriter(args[1]));
			
			try{
				
				fileWriter.write("Output 1:\n");
				
				// Write sorted by gender and last name
				Collections.sort(peopleList, new GenderComparator());
				
				for(int i = 0; i < peopleList.size(); i++)
				{
					fileWriter.write(peopleList.get(i) + "\n");
				}
				
				fileWriter.write("\nOutput 2:\n");
				
				// Write sorted by D.O.B
				Collections.sort(peopleList, new DateComparator());
				
				for(int i = 0; i < peopleList.size(); i++)
				{
					fileWriter.write(peopleList.get(i) + "\n");
				}
				
				fileWriter.write("\nOutput 3:\n");
				
				// Write sorted by last name in descending order
				Collections.sort(peopleList, new LastNameComparator());
				
				for(int i = 0; i < peopleList.size(); i++)
				{
					fileWriter.write(peopleList.get(i) + "\n");
				}
				
			}
			finally
			{
				fileWriter.close();
			}
			
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
}
